#ifndef _GWBASE_BASE_THREAD_POOL_H_
#define _GWBASE_BASE_THREAD_POOL_H_

#include "gwbase/base/atomic.h"
#include "gwbase/base/condvar.h"
#include "gwbase/base/mutex.h"
#include "gwbase/base/thread.h"
#include <vector>
#include <deque>

namespace dicom {

class ThreadPool {
 public:
  typedef boost::function<void()> Task;

  ThreadPool(const std::string& name, int max_threads);
  ~ThreadPool();

  void Start();
  void Stop();

  // add task to queue
  void RunTask(const Task& t);

  int max_threads() const { return max_threads_; }

 private:
  void Loop();
  Task GetNextTask();

  std::string name_;
  int max_threads_;
  bool stopping_;
  bool started_;
  std::deque<Task> tasks_;
  std::vector<ThreadPtr> threads_;
  Mutex mu_;
  CondVar not_empty_;
  CondVar not_full_;

  AtomicInt32 count_;
};
}

#endif  // _GWBASE_BASE_THREAD_POOL_H_
